package com.share.web.action.mail;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.share.mail.InBoxMailManager;
import com.share.mail.InBoxMailVo;
import com.share.mail.MailInfoVO;
import com.share.mail.SendMailManager;
import com.share.web.action.base.BaseActionSupport;

@Controller
@Scope("prototype")
public class WebMailAction extends BaseActionSupport {

	private static final long serialVersionUID = 1L;
	
	private boolean result ;

	private InBoxMailVo inbox ;
	
	private MailInfoVO miv;

	@Resource
	private SendMailManager smm;

	@Resource
	private InBoxMailManager inboxMgr ;

	public InBoxMailVo getInbox() {
		return inbox;
	}
	
	/** 读取所有邮件列表--驱动模型，将数据保存到Session范围，返回的是JSON，异步交互 */
	public String readMailList() {
		try {
			InBoxMailVo inbox = inboxMgr.inboxMailMessage("serup_server","serup_server123", "readList", null, 0,this.getBaseform().getRealPath()) ;
			this.getBaseform().getRequest().getSession().setAttribute("inbox", inbox) ;
			if(null != inbox) {
				result = true ;
			} else {
				result = false ;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "readList" ;
	}
	
	/** readMailListJSON,openMail,sendMailAction方法都是用于Ajax版本 */
	
	/** 读取所有邮件列表 */
	public String readMailListJSON() {
		try {
			inbox = inboxMgr.inboxMailMessage("serup_server","serup_server123", "readList", null, 0, this.getBaseform().getRealPath()) ;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "readListJSON" ;
	}
	
	/** 删除选定邮件 */
	public String deleteMailListJSON() {
		try {
			result = inboxMgr.inboxMailMessage("serup_server","serup_server123", "delMail", this.getBaseform().getParam(), 0, this.getBaseform().getRealPath()).isDelConfir() ;
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "deleteMailJSON" ;
	}
	
	/** 打开某封邮件 */
	public String openMail() {
		try {
			this.getBaseform().getRequest().setAttribute("mail",inboxMgr.inboxMailMessage("serup_server","serup_server123", null, null, Integer.parseInt(this.getBaseform().getParam()),this.getBaseform().getRealPath())) ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
		return "openMail" ;
	}

	/** 发送邮件 */
	public String sendMailAction() {
		
		miv.setAliasName("孤独的狼") ;
		miv.setUsername("serup_server") ;
		miv.setPassword("serup_server123") ;
		miv.setTemplateFileName("test.ftl") ;
		
		result = smm.sendMailMessages(miv,this.getBaseform().getRequest().getSession().getServletContext()) ;

		return "senderMail";
	}
	
	@JSON(serialize=false,deserialize=false)
	public MailInfoVO getMiv() {
		return miv;
	}

	public boolean isResult() {
		return result;
	}
	
	public void setInbox(InBoxMailVo inbox) {
		this.inbox = inbox;
	}

	public void setMiv(MailInfoVO miv) {
		this.miv = miv;
	}

	public void setResult(boolean result) {
		this.result = result;
	}

	public void setSmm(SendMailManager smm) {
		this.smm = smm;
	}

}
